// import { Effect } from 'umi'
import {
  facListAll, // 全部厂别
  deviceTypeList, // 全部感测器类型
  getCodeDescByType, // 全部分类
} from '@/pages/setting/setServers';
import pathToRegexp from 'path-to-regexp';

type Dispatch = () => {};
type History = {
  [key: string]: any;
};
interface FacList {
  cname: string;
  ename: string;
  isactive: string;
  siteid: string;
}

/* dva setting 全局数据 */
export default {
  // 表示全局 state 上的 key，命名空间
  namespace: 'setting',
  // 初始值
  state: {
    num: 0,
    facAll: [],
    // 全部厂别
    facListAll: [],
    // 全部感测器类型
    deviceTypeListAll: [],
    // 感测器状态
    stateTypeListAll: [
      {
        Code: 'STATUS01',
        ENUS: null,
        Name: null,
        Remark: '启用',
        Type: null,
        ZHTW: null,
      },
      {
        Code: 'STATUS02',
        ENUS: null,
        Name: null,
        Remark: '关闭',
        Type: null,
        ZHTW: null,
      },
      {
        Code: 'STATUS06',
        ENUS: null,
        Name: null,
        Remark: '作废',
        Type: null,
        ZHTW: null,
      },
    ],
    // 分类
    sensorTypeListAll: [],
  },

  // 等同于 redux 里的 reducer, 处理同步操作
  // 更新store，用新数据合并state的旧数据
  reducers: {
    increase(state: any) {
      console.log('increase', state.num);
      return {
        ...state,
        num: state.num + 1,
      };
    },
    decrease(state: any) {
      console.log('是否触发 decrease 方法');
      return {
        ...state,
        num: state.num - 1,
      };
    },
    add(state: any, { payload }: { payload: any }) {
      // console.log('onAdd 中的数据', payload, typeof payload)
      return {
        ...state,
        num: state.num + payload,
      };
    },
    // getFactory(state: any) {
    //   console.log('获取全部厂别', state);
    //   return state.facAll.push('F0', 'F1');
    // },
    setFacListAll(state: any, { payload }: { payload: [FacList] }) {
      // console.log('state payload 设置数据》》》', state, payload)
      const facList: FacList[] = [];
      payload.length &&
        payload.forEach((item: FacList) => {
          facList.push(item);
        });
      // console.log(facList, 'facList')
      return {
        ...state,
        facListAll: facList,
      };
    },
    setDeviceType(state: any, { payload }: { payload: any }) {
      return {
        ...state,
        deviceTypeListAll: payload,
      };
    },
    setSensorType(state: any, { payload }: { payload: any }) {
      // console.log('分类数据处理', payload)
      return {
        ...state,
        sensorTypeListAll: payload,
      };
    },
  },

  // 用于处理异步操作（例如：与服务端交互）由 action 触发,不可以修改 state
  effects: {
    // 副作用操作，函数的名字就是匹配的 action
    *getFacListAll({ payload }: any, obj: any): any {
      try {
        // Generator
        // 异步请求接口
        const datas = yield obj.call(facListAll, '/user');
        // 调用reducer
        yield obj.put({ type: 'setFacListAll', payload: datas.data });
      } catch (error) {}
      // console.log('执行了 getFacListAll', payload, obj, 123, datas )
    },
    *getDeviceTypeAll({ payload }: any, obj: any): any {
      try {
        const deviceDatas = yield obj.call(deviceTypeList);
        // console.log('全部的感测器类型', deviceDatas)
        yield obj.put({ type: 'setDeviceType', payload: deviceDatas['data'] });
      } catch (error) {}
    },
    *getSensorTypeAll({ payload }: any, obj: any): any {
      // console.log("传递过来的 数据",  payload, )
      try {
        const sensortype = yield obj.call(getCodeDescByType, payload);
        // console.log('全部的分类', sensortype)
        yield obj.put({ type: 'setSensorType', payload: sensortype['data'] });
      } catch (error) {}
    },
    *asyncIncrease(action: any, obj: any) {
      console.log('effects!!!', action, obj);
      // 触发
      yield obj.put({ type: 'increase' });
    },
    *asyncDecrease(action: any, obj: any) {
      console.log('effects!!! decrease', action, obj);
      // 触发
      yield obj.put({ type: 'decrease' });
    },
  },

  // 设置订阅 会在模型加入 *仓库后* 理解执行
  // 只执行一次
  subscriptions: {
    // 初始化的时候加载全部的 facList
    getFacList({ dispatch }: { dispatch: Function }) {
      // console.log('初始化 获取全部厂别数据！！！');
      dispatch({ type: 'getFacListAll' });
      dispatch({ type: 'getDeviceTypeAll' });
      dispatch({ type: 'getSensorTypeAll', payload: { type: 'ST' } });
    },
    m1({ dispatch, history }: { dispatch: Dispatch; history: History }) {
      // 路由监听
      history.listen(() => {
        // console.log('m1 判断路由 执行', history);
        const { pathname } = history['location'];
        const match = pathToRegexp('/AreaMtnSet/:buildingId').exec(pathname);
        // console.log(pathname, match, 'location>>>>>>>>>>>>>>>>>>>>')
      });
    },
    m2() {
      // console.log('m2 执行了');
    },
    resizeIncrease({ dispatch }: { dispatch: any }) {
      // 订阅窗口尺寸变化，
      window.onresize = () => {
        // dispatch({ type: 'increase' });
      };
    },
  },

  // 是 reducers 及 effects 的触发器
  // action: {},
};

/* // 得到一个 dva 对象
const app = dva()
// 在启动之前定义模型
app.model("模型名称")
app.model(counterModel)
// 设置根路由，既启动后，要运行的函数，函数的返回结果会被渲染
app.router(()=> <App />)
app.start("#root") */
